#encoding:utf-8

import mysql.connector


def insert(*self):
	mysqldb = mysql.connector.connect(user = 'admins',passwd = '123',host = 'localhost',database = 'user_system',buffered = True)#mysql服务器信息
	mydb = mysqldb.cursor()#设置游标
	mydb.execute('SELECT user_name FROM user_information')
	var = mydb.fetchall()
	def judge(ls):
		for i in ls:
			#print('类型为:{} 数据为:{}'.format(type(i),i))
			#print('类型为:{} 数据为:{}'.format(type(self[0]),self[0]))
			if i[0] == self[0]:
				return 'False'
	rn_data = judge(var)
	if rn_data == 'False':
		return '用户名重复,请重新输入!'
	else:
		try:
			ins = "INSERT INTO user_information(user_name,user_passwd,user_tel,user_mail) VALUES (%s,%s,%s,%s)"
			mydb.execute(ins,self)#插入数据
			mysqldb.commit()#更新数据库
			print(mydb.rowcount,"数据插入成功")
			return '注册成功'
		except:
			return '注册失败,请重试!'
	mydb.close()

def query(*self):
	mysqldb = mysql.connector.connect(user = 'admins',passwd = '123',host = 'localhost',database = 'user_system',buffered = True)#mysql服务器信息
	mydb = mysqldb.cursor()#设置游标
	mydb.execute('SELECT user_name,user_passwd FROM user_information')
	var = mydb.fetchall()
	v = len(var)
	m = var[v-1]
	for x in var:
		#self=self[0]
		#print(self)
		#print(x)
		if x == self:
			return '核对成功'
		elif x == m:
			return '核对失败'
	mydb.close()


if __name__ == '__main__':
	Insert()





#db_sql.execute("CREATE TABLE user_information(user_id INT(96) NOT NULL AUTO_INCREMENT,user_name VARCHAR(128) NOT NULL,user_passwd VARCHAR(128) NOT NULL,user_tel INT(15) NOT NULL,user_mail VARCHAR(128) NOT NULL,PRIMARY KEY (user_id))ENGINE = InnoDB")#创建数据表


